package com.jufa_3.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jufa_3.dto.SelectInfo;
import com.jufa_3.entity.MonitoringInfo;
import com.jufa_3.entity.MonitoringTask;
import com.jufa_3.mapper.MonitoringInfoMapper;
import com.jufa_3.service.MonitoringTaskService;
import com.jufa_3.service.SelectInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class Monitoring {

    @Autowired
    private MonitoringInfoMapper monitoringInfoMapper;

    @Autowired
    private MonitoringTaskService monitoringTaskService;

    @Autowired
    private SelectInterface selectInterface;

    @Autowired
    private StringRedisTemplate redisTemplate;

    public static String dataTypes[] = {"ktgg", "zxgg", "shixin", "fygg", "bgt", "cpws", "ajlc", "wdhmd"};

    public static String dataTypesCN[] = {"开庭公告", "执行公告", "失信公告", "法院公告", "曝光台", "裁判文书", "案件流程", "黑名单"};

    //    @Scheduled(fixedDelay = 1000*60*604800)
    public void sayHello() {
        // 拉取列表
        log.warn("任务启动...................");
        List<MonitoringTask> monitoringTaskList = monitoringTaskService.lambdaQuery()
                .eq(MonitoringTask::getEnable, "1")
                .ge(MonitoringTask::getEndDate, LocalDateTime.now())
                .list();
        for (MonitoringTask mt : monitoringTaskList) {
            log.warn("执行监控任务： " + mt.toString());
            if ("personal".equals(mt.getType())) {
                // 拉取最新数据 和 源数据比对
                String updataMsg = "暂未监测到！";
                String[] split = mt.getNameOrKeyword().split("&");
                String name = split[0];
                String cardNum = split[1];
                String mobile = split[2];
                SelectInfo selectInfo = new SelectInfo(
                        mt.getOpenId(),
                        name,
                        cardNum,
                        mobile,
                        mt.getNameOrKeyword(),
                        mt.getPayId(),
                        "MONI_SE"
                );
                // 1.获取以前的
                JSONObject oldInfo = selectInterface.zewuFyxxxq(selectInfo);
                // 2.删除以前的
                redisTemplate.delete("NC::" + mt.getNameOrKeyword());
                // 3.获取新的
//                JSONObject newInfo = JSONObject.parseObject(redisTemplate.boundValueOps("keys").get());
                JSONObject newInfo = selectInterface.zewuFyxxxq(selectInfo);
                // 4.新旧对比
                if (newInfo.getInteger("code") == 0) {
                    JSONObject oldCourtDetailPro = oldInfo.getJSONObject("data").getJSONObject("CourtDetailPro");
                    JSONObject newCourtDetailPro = newInfo.getJSONObject("data").getJSONObject("CourtDetailPro");
                    for (int i = 0; i < dataTypes.length; i++) {
                        JSONArray oldEntrys = oldCourtDetailPro.getJSONArray(dataTypes[i]);
                        JSONArray newEntrys = newCourtDetailPro.getJSONArray(dataTypes[i]);
                        // 核对数据
                        int min = (newEntrys == null ? 0 : newEntrys.size()) - (oldEntrys == null ? 0 : oldEntrys.size());
                        if (min > 0) {
                            updataMsg = dataTypesCN[i] + "新增" + min + "条";
                            monitoringInfoMapper.insert(new MonitoringInfo(mt.getId(), LocalDateTime.now(), updataMsg));
                        } else if (min < 0) {
                            updataMsg = dataTypesCN[i] + "减少" + min * -1 + "条";
                            monitoringInfoMapper.insert(new MonitoringInfo(mt.getId(), LocalDateTime.now(), updataMsg));
                        }
                    }
                } else {
                    // 数据失效
                    updataMsg = newInfo.getString("desc");
                    // 添加核对日志 保存核对数据
                    monitoringInfoMapper.insert(new MonitoringInfo(mt.getId(), LocalDateTime.now(), updataMsg));
                }
            } else if ("comPost".equals(mt.getType())) {

            } else if ("comInfo".equals(mt.getType())) {

            }
        }
        log.warn("任务结束...................");
    }

}
